package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.utils.UserContext;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.StrategySunComment;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.query.StrategySunCommentQueryObject;
import cn.wolfcode.trip.base.query.TagQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.utils.JSONResult;
import com.fasterxml.jackson.annotation.JsonRawValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import java.util.Date;

@Controller
@RequestMapping("/strategyComments")
public class StrategyCommentController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategySunCommentService sunCommentService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private ITagService tagService;
    @Autowired
    private ServletContext ctx;



    //获取单个点评详情
    @GetMapping("/{id}")
    @ResponseBody
    public Object getStrategyCommentById(@PathVariable Long id){
        return strategyCommentService.selectStrategyCommentById(id);
    }

    @GetMapping("/{strategyCommentId}/sunComments")
    @ResponseBody
    public Object getStrategySunComments(@ModelAttribute("qo") StrategySunCommentQueryObject qo){
        //qo.setPageSize(0);
        return sunCommentService.query(qo);
    }

    @PostMapping("/sunComments")
    @ResponseBody
    public Object saveStrategySunComment(StrategySunComment sunComment){
        JSONResult result = new JSONResult();
        try{
            sunComment.setLikeNum(10);
            sunComment.setCreateTime(new Date());
            sunComment.setUser(UserContext.getCurrentUser());
            sunCommentService.save(sunComment);
        }catch(Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;

    }
    @GetMapping("/{strategyCommentId}/isExist")
    @ResponseBody
    public Object isExist(@PathVariable Long strategyCommentId,Long userId){

        JSONResult result = new JSONResult();

        if (userId!=null){

            if (strategyCommentService.queryIfexist(strategyCommentId,userId)==1){
                return result;
            }else {
                result.setSuccess(false);
                return result;
            }
        }else {
            result.setSuccess(false);
            return result;
        }
    }
    @PostMapping("/like/saveLike")
    @ResponseBody
    public Object saveLike(Long strategyCommentId,Long userId){
        JSONResult result = new JSONResult();
        try {
            strategyCommentService.saveLike(userId,strategyCommentId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    @DeleteMapping("/like/deleteLike")
    @ResponseBody
    public Object delete( Long strategyCommentId,Long userId){
        JSONResult result = new JSONResult();
        try {
            strategyCommentService.deleteLike(userId,strategyCommentId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    @GetMapping("/{strategyCommentId}/likeCount")
    @ResponseBody
    public Object getLikeCount(@PathVariable Long strategyCommentId,Long userId){
        Integer count = strategyCommentService.queryLikeCount(strategyCommentId);
        JSONResult result = new JSONResult();
        result.setObj(count);
        if (userId!=null){

            if (strategyCommentService.queryIfexist(strategyCommentId,userId)==1){
                return result;
            }else {
                result.setSuccess(false);
                return result;
            }
        }else {
            result.setSuccess(false);
            return result;
        }
    }
}
